package org.jeecg.modules.demo.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.checkerframework.checker.units.qual.A;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.crm.entity.CSubProject;
import org.jeecg.modules.demo.crm.entity.CTask;
import org.jeecg.modules.demo.crm.mapper.CTaskMapper;
import org.jeecg.modules.demo.crm.service.ICCommonService;
import org.jeecg.modules.demo.crm.service.ICTaskService;
import org.jeecg.modules.demo.crm.util.execl.ExcelWriterUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description: 任务功能
 * @Author: jeecg-boot
 * @Date:   2025-06-16
 * @Version: V1.0
 */
@Service
public class CTaskServiceImpl extends ServiceImpl<CTaskMapper, CTask> implements ICTaskService {

    @Autowired
    CTaskMapper cTaskMapper;

    @Autowired
    ICCommonService commonService;

    @Override
    public void edit(CTask cTask) {
        cTaskMapper.updateById(cTask);
    }

    @Override
    public void turnDown(CTask cTask) {
        CTask cTask1 = cTaskMapper.selectById(cTask.getId());
        if("合格".equals(cTask.getProcessingStatus())){
            cTask1.setProcessingStatus("完成");
        }else {
            cTask1.setProcessingStatus("驳回");
            cTask1.setProcessingDate(null);
            cTask1.setAttachment("");
            cTask1.setProcessingResult("");
        }
        cTaskMapper.updateById(cTask1);
    }

    @Override
    public void dealWith(CTask cTask) {
        CTask cTask1 = cTaskMapper.selectById(cTask.getId());
        cTask1.setProcessingStatus("已处理");
        cTask1.setProcessingDate(cTask.getProcessingDate());
        cTask1.setAttachment(cTask.getAttachment());
        cTask1.setProcessingResult(cTask.getProcessingResult());
        cTaskMapper.updateById(cTask1);
    }

    @Override
    public void process(CTask cTask) {
        CTask cTask1 = cTaskMapper.selectById(cTask.getId());
        cTask1.setProcessingStatus("已处理");
        cTask1.setProcessingDate(cTask.getProcessingDate());
        cTask1.setProcessingResult(cTask.getProcessingResult());
        cTask1.setAttachment(cTask.getAttachment());
        cTaskMapper.updateById(cTask1);
    }

    @Override
    public void acceptanceEditEdit(CTask cTask) {
        CTask cTask1 = cTaskMapper.selectById(cTask.getId());
       // cTask1.setProcessingStatus("已处理");
        cTask1.setProcessingDate(cTask.getProcessingDate());
        cTask1.setProcessingResult(cTask.getProcessingResult());
        cTask1.setAttachment(cTask.getAttachment());
        cTask1.setServiceUnit(cTask.getServiceUnit());
        cTask1.setName(cTask.getName());
        cTask1.setProjectNo(cTask.getProjectNo());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");

        String processingDate = sdf.format(cTask.getProcessingDate());
        // 使用 split 方法按逗号分割字符串
        String[] items = cTask.getAttachment().split(",");

        // 将数组转换为 List<String>
        List<String> imagePaths = new ArrayList<>(Arrays.asList(items));
        if (StringUtils.isEmpty(cTask.getContactPerson())) {
            return;
        }

        //生成 验收单
        ExcelWriterUtils.writer(cTask.getServiceUnit(),
                cTask.getName(),cTask.getProjectNo(),
                processingDate,
                imagePaths,
                cTask.getProcessingResult(),
                cTask.getContactPerson(),cTask1);
        cTaskMapper.updateById(cTask1);
    }

    @Override
    public Map<String, Long> statusCount(CTask task, HttpServletRequest req) {
        CTask query = new CTask();
        Map<String, Long> data = new HashMap<>();
        List<String> statusList = new ArrayList<>();
        statusList.add("待处理");
        statusList.add("已处理");
        statusList.add("驳回");
        statusList.add("完成");
        statusList.add("全部");
        QueryWrapper<CTask> queryWrapper = null;
        Boolean permissionCheck = commonService.taskpermissionCheck();
        for (String status : statusList) {
            queryWrapper = QueryGenerator.initQueryWrapper(query, req.getParameterMap());
            if(!status.equals("全部")){
                queryWrapper.eq("processing_status", status);
            }
            if(task != null && task.getProjectId() != null){
                queryWrapper.eq("project_id",task.getProjectId());
            }
            if(task != null && task.getSubProjectId()!= null){
                queryWrapper.eq("sub_project_id",task.getSubProjectId());
            }
            if(permissionCheck){
                LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
                queryWrapper.eq("assigned_to",sysUser.getId());
            }
            data.put(status,cTaskMapper.selectCount(queryWrapper));
        }
        return data;
    }

    @Override
    public void included(CTask cTask) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        cTask.setProcessingStatus("已处理");
        cTask.setAssignedTo(sysUser.getId());
        cTask.setTaskDate(cTask.getProcessingDate());
        cTask.setContent(cTask.getProcessingResult());
        cTaskMapper.insert(cTask);


    }
}
